import Vue from 'vue'
import VueRouter from 'vue-router'
// import Layout from '@/views/layout'
// import Login from '@/views/login'
// import Register from '@/views/register'
// import Actile from '@/views/acticle'
// import UserIformation from '@/views/Userinfo'
// 二级路由
// import Home from '@/views/layout/home'
// import Mine from '@/views/layout/mine'
// import Publish from '@/views/layout/publish'
// import Sort from '@/views/layout/sort'
// import sortList from '@/views/sortList'
// import Myarticle from '@/views/layout/mine/Myarticle'
// import Mycollect from '@/views/layout/mine/Mycollect'
// import Mylove from '@/views/layout/mine/Mylove'
import store from '@/store'
import { Toast } from 'vant'
Vue.use(VueRouter)
const routes = [
  {
    path: '/',
    component: () => import('@/views/layout'),
    redirect: 'home',
    children: [
      {
        path: 'home',
        component: () => import('@/views/layout/home'),
        meta: {
          isKeepAlive: true
        }
      },
      {
        path: 'mine',
        component: () =>
          import(/* webpackChunkName:"mine" */ '@/views/layout/mine')
      },
      {
        path: 'publish',
        component: () => import('@/views/layout/publish')
      },
      {
        path: 'sort',
        component: () => import('@/views/layout/sort'),
        meta: {
          isKeepAlive: true
        }
      }
    ]
  },
  {
    path: '/login',
    component: () => import('@/views/login')
  },
  {
    path: '/register',
    component: () => import('@/views/register')
  },
  {
    path: '/actile',
    component: () => import('@/views/acticle')
  },
  {
    path: '/myarticle',
    component: () =>
      import(/* webpackChunkName:"mine" */ '@/views/layout/mine/Myarticle')
  },
  {
    path: '/mycollect',
    component: () =>
      import(/* webpackChunkName:"mine" */ '@/views/layout/mine/Mycollect')
  },
  {
    path: '/mylove',
    component: () =>
      import(/* webpackChunkName:"mine" */ '@/views/layout/mine/Mylove')
  },

  {
    path: '/sort/list',
    component: () => import('@/views/sortList')
  },
  {
    path: '/userIformation',
    component: () => import('@/views/Userinfo')
  },
  {
    path: '*',
    component: () => import('@/views/404')
  }
]

const router = new VueRouter({
  routes
})

const loginURL = ['/myarticle', '/mycollect', '/mylove']
router.beforeEach((to, from, next) => {
  const token = store.state.user.token

  if (token.length > 0) {
    next()
  } else {
    if (loginURL.some((item) => to.path === item)) {
      // if成立，需要登录
      // next('/login')
      router.push('/login')
      Toast('请先登录')
    } else {
      next()
    }
  }
})
export default router
